//
//  LuxuryFiltrateResultViewController.m
//  DuBai
//
//  Created by darktm on 14-2-26.
//  Copyright (c) 2014年 darktm. All rights reserved.
//

#import "LuxuryFiltrateResultViewController.h"
#import "LuxuryFiltrateResultViewCell.h"

#import "FiltrateViewController.h"
#import "TaskWithFindGoodsByKey.h"

#import "SVPullToRefresh.h"

@interface LuxuryFiltrateResultViewController ()<FiltrateViewControllerDelegate,WYHttpTaskDelegate>

@property(nonatomic,strong)NSMutableArray *goodsArray;

@end

@implementation LuxuryFiltrateResultViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
    }
    return self;
}

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view.
    
    self.title = self.keyword;
    
    self.againstBtn.layer.cornerRadius = 13.0f;
    self.againstBtn.layer.masksToBounds = YES;
    
    [self.popularityBtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.popularityBtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    [self.popularityBtn setSelected:YES];
    
    
    [self.priceBtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.priceBtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    
    [self.salesBtn setTitleColor:[UIColor brownColor] forState:UIControlStateSelected];
    [self.salesBtn setTitleColor:[UIColor brownColor] forState:UIControlStateHighlighted];
    
    [self loadSearchs];
    
}


- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    
    return self.goodsArray.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    LuxuryFiltrateResultViewCell *cell = [tableView dequeueReusableCellWithIdentifier:@"LuxuryFiltrateResultViewCell" forIndexPath:indexPath];
    
    cell.originPrice.attributedText = [[NSMutableAttributedString alloc] initWithString:@"RMB 99999" attributes:[NSDictionary dictionaryWithObjectsAndKeys:@1,NSStrikethroughStyleAttributeName,[[UIColor darkTextColor] colorWithAlphaComponent:.5],NSForegroundColorAttributeName,nil]];
    return cell;
}

#pragma mark - http delegate -

-(void)requestCompleteWithObj:(WYHttpTask *)obj{
    
    id rsult = [obj parese];
    
    if([obj isKindOfClass:[TaskWithFindGoodsByKey class]]){
        
        if(self.goodsArray){
            [self.goodsArray addObjectsFromArray:rsult];
        }else{
            self.goodsArray = rsult;
        }
    }
    [self.tableView.infiniteScrollingView stopAnimating];
    [self.tableView.pullToRefreshView stopAnimating];
    [self.tableView reloadData];
    
}
-(void)requestFailWithMsg:(WYHttpTask *)obj{
    DLog(@"%@",obj.networkOp.url);
    [self.tableView.infiniteScrollingView stopAnimating];
    [self.tableView.pullToRefreshView stopAnimating];
}



- (void)didReceiveMemoryWarning
{
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


-(void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender{
    
    if([segue.identifier isEqualToString:@"FiltrateView"]){//商品详情页面
        //赋值
        FiltrateViewController *detail = segue.destinationViewController;
        detail.delegate = self;
    }
}

-(void)didSeletedValues:(id)values{
    DLog(@"%@",values);
//    load values
    
    self.title = [values localizedAdditionalDescription];
}

#pragma mark - private mothod -

-(void)loadSearchs{
    
    TaskWithFindGoodsByKey *g = [[TaskWithFindGoodsByKey alloc] init];
    g.start = [NSString stringWithFormat:@"%d",self.goodsArray.count ? : 0];
    g.key = self.title;
    g.delegate = self;
    [g start];
}

#pragma mark - btn action -

- (IBAction)on_btn_populatity:(UIButton *)sender {
    if(sender.selected) return;
    self.popularityBtn.selected = YES;
    self.priceBtn.selected = NO;
    self.salesBtn.selected = NO;
    [self promptyAnimationIndex:0];
}

- (IBAction)on_btn_price:(UIButton *)sender {
    if(sender.selected) return;
    self.popularityBtn.selected = NO;
    self.priceBtn.selected = YES;
    self.salesBtn.selected = NO;
    [self promptyAnimationIndex:1];
}

- (IBAction)on_btn_sales:(UIButton *)sender {
    if(sender.selected) return;
    self.popularityBtn.selected = NO;
    self.priceBtn.selected = NO;
    self.salesBtn.selected = YES;
    [self promptyAnimationIndex:2];
}

-(void)promptyAnimationIndex:(int)index{
    CGRect frame = self.prompted.frame;
    frame.origin.x = 20 + index * 107;
    
    [UIView animateWithDuration:.2 delay:0 options:UIViewAnimationOptionBeginFromCurrentState animations:^{
        self.prompted.frame = frame;
    } completion:NULL];
}

@end
